﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Infrastructure.Authentication;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;

namespace XSTDZ.ERP.WebApp.Controllers
{
     [NoAuthorizated]
    public class DeptController : Controller
    {
         private readonly IOrgService orgServiceImpl = ServiceLocator.Instance.GetService<IOrgService>();	
        //
        // GET: /Dept/

        public ActionResult Add(string id, string orgname,bool isorg)
        {
            ViewBag.IsOrg = isorg?"none":"";
            if (!isorg)//点击的是部门
            {
                ViewBag.ParentId = id ?? "";
                ViewBag.ParentName = orgname ?? "";
                ViewBag.OrgDto= orgServiceImpl.GetOrgByDeptId(Guid.Parse(id));
            }
            else//组织机构
            {
                ViewBag.ParentId =  "";
                ViewBag.ParentName =  "";
                ViewBag.OrgDto = new OrgDto() {
                    Id = Guid.Parse(id),
                     Name=orgname
                };
            }
            return View();
        }
         [HttpPost]
        public ActionResult Add(DeptDto deptDto)
        {
            if (ModelState.IsValid)
            {
                orgServiceImpl.AddDept(deptDto);
                ViewBag.PostState = 9;

            }
            ViewBag.OrgDto = new OrgDto()
            {
                Id =deptDto.OrgId,
                Name = HttpContext.Request.Form["OrgName"]
            };
            return View();
        }
         //更新部门
         public ActionResult Edit(string id, string orgname,bool isorg)
         {
             ViewBag.IsOrg = isorg ? "none" : "";
             if (!isorg)//点击的是部门
             {
                 ViewBag.ParentId = id ?? "";
                 ViewBag.ParentName = orgname ?? "";
                 ViewBag.OrgDto = orgServiceImpl.GetOrgByDeptId(Guid.Parse(id));
             }
             else//组织机构
             {
                 ViewBag.ParentId = "";
                 ViewBag.ParentName = "";
                 ViewBag.OrgDto = new OrgDto()
                 {
                     Id = Guid.Parse(id),
                     Name = orgname
                 };
             }

             var dept = orgServiceImpl.GetDept(Guid.Parse(id));
             return View(dept);
         }
         [HttpPost]
         public ActionResult Edit(DeptDto deptDto)
         {
             if (ModelState.IsValid)
             {
                 orgServiceImpl.UpdateDept(deptDto);
                 ViewBag.PostState = 9;
             }
             ViewBag.OrgDto = new OrgDto()
             {
                 Id = deptDto.OrgId,
                 Name = HttpContext.Request.Form["OrgName"]
             };
             return View();
         }

         [HttpGet]
         public ActionResult Delete(string id)
         {
             orgServiceImpl.DeleteDept(Guid.Parse(id));
             return Content("ok");
         }
    }
}
